// guoba.support.js
import path from "path";
import setting from "./model/setting.js";
import lodash from "lodash";

const _path = path.join(process.cwd(), "plugins", "dsplugin-plugin");

/**
 * 支持锅巴配置
 */
export function supportGuoba() {
  return {
    pluginInfo: {
      name: "dsplugin-plugin",
      title: "云汐插件",
      author: "@云汐",
      authorLink: "https://gitee.com/YunXi-67",
      link: "https://gitee.com/YunXi-67/nodeutilities",
      isV3: true,
      isV2: false,
      description: "提供多个功能",
      icon: "mdi:stove",
      iconColor: "#d19f56",
      iconPath: path.join(_path, "resources", "img", "tb.jpg"),
    },
    // 配置项信息
    configInfo: {
      schemas: [
        {
          field: 'config.DEEQSEEKTOKEN',
          label: 'deepseekUserToken',
          bottomHelpMessage: "deepseek的用户token",
          component: 'Input',
          required: false,
          componentProps: {
            placeholder: "请输入UserToken",
          }
        },
        {
          field: 'config.tokenExpirationTime',
          label: 'token过期时间',
          bottomHelpMessage: "deepseek的token过期时间，单位为秒，默认为3600秒",
          component: 'Input',
          required: false,
          componentProps: {
            placeholder: "请输入token过期时间",
          }
        },
        {
          field: 'config.shouldSaveCookie',
          label: '开启cookie缓存',
          bottomHelpMessage: '开启或关闭cookie缓存都不会影响请求，默认关闭',
          component: "Select",
          componentProps: {
            options: [
              { label: '关闭', value: false },
              { label: '开启', value: true },
            ],
            placeholder: "请选择是否开启",
          }
        },
        {
          field: 'config.cookieExpirationTime',
          label: 'cookie过期时间',
          bottomHelpMessage: 'cookie过期时间，单位为秒，默认为3600秒, 关闭cookie缓存后该选项无效',
          component: 'Input',
          required: false,
          componentProps: {
            placeholder: "请输入cookie过期时间",
          }
        },
        {
          field: 'config.shouldEnableImageCompose',
          label: '开启图片合成',
          bottomHelpMessage: '开启或关闭图片合成，默认开启',
          component: "Select",
          componentProps: {
            options: [
              { label: '关闭', value: false },
              { label: '开启', value: true },
            ],
            placeholder: "请选择是否开启",
          }
        }
      ],
      getConfigData() {
        try {
          return setting.merge();
        } catch (error) {
          logger.error("获取配置数据失败:", error);
          throw error;
        }
      },
      setConfigData(data, { Result }) {
        try {
          let config = {};
          for (let [keyPath, value] of Object.entries(data)) {
            lodash.set(config, keyPath, value);
          }
          config = lodash.merge({}, setting.merge(), config);
          setting.analysis(config);
          return Result.ok({}, '保存成功~');
        } catch (error) {
          logger.error("保存配置数据失败:", error);
          return Result.error({}, '保存失败~');
        }
      }
    }
  };
}